﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Rabbit.API.Application.Command.Goods;
using System.Text;
using Yitter.IdGenerator;

namespace Rabbit.API.Application.Handler.Goods
{
    /// <summary>
    /// 
    /// </summary>
    public class GoodsAddHandler : IRequestHandler<GoodsAddCommand,APIResult<int>>
    {
        private readonly IRepository<GoodsModel> goodsway;
        private readonly ILogger<GoodsAddHandler> log;
        private readonly IMapper mapper;

        public GoodsAddHandler(IRepository<GoodsModel> goodsway, ILogger<GoodsAddHandler> log, IMapper mapper)
        {
            this.goodsway = goodsway;
            this.log = log;
            this.mapper = mapper;
        }
        /// <summary>
        /// 商品添加
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        public async Task<APIResult<int>> Handle(GoodsAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            try
            {
                var goods = mapper.Map<GoodsModel>(request);  //参数类型
                var Proportion = 0;
                if (!string.IsNullOrEmpty(request.GoodsName))
                {
                    Proportion += 15;
                }
                if (!string.IsNullOrEmpty(request.CreateBatch))
                {
                    Proportion += 15;
                }
                if (!string.IsNullOrEmpty(request.CreateCode))
                {
                    Proportion += 15;
                }
                if (!string.IsNullOrEmpty(request.CreateCompany))
                {
                    Proportion += 15;
                }
                if (!string.IsNullOrEmpty(request.Covers))
                {
                    Proportion += 15;
                }
                if (!string.IsNullOrEmpty(request.Tests))
                {
                    Proportion += 15;
               
                }
                if (request.ExpireTime!=null)
                {
                    Proportion += 10;
                }
                goods.Proportion = Proportion.ToString()+"%";
                goods.Id = YitIdHelper.NextId();  //主键雪花
               
                //Random rd = new Random();  //伪随机数实例
                //StringBuilder numbers = new StringBuilder();        
                //for (int i = 0; i < 14; i++)
                //{
                //    numbers.Append( rd.Next(0,10));
                //}
                //goods.CreateCode = "SC" + numbers;  //SC+ 14位数字许可证号
                //goods.CreateBatch = DateTime.Now.ToString("yyyyMMdd");  //获取生产批次
                var result = await goodsway.Addway(goods);  //添加
                if(result>0)
                {
                    log.LogInformation($"商品{request.GoodsName},在{DateTime.UtcNow}添加成功");
                    res.Message = $"商品{request.GoodsName},在{DateTime.UtcNow}添加成功";
                    res.Num = StateCode.OK;
                    res.Data = result;
                }
                else
                {
                    res.Message = $"商品{request.GoodsName},在{DateTime.UtcNow}添加失败";
                    res.Num = StateCode.Fail;
              
                }
                return res;
 
            }
            catch (Exception ex)
            {
                log.LogError($"登录时发生异常{ex.Message}");
                throw;
            }
        }
    }
}
